﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.zynga.skelly.render {
    import flash.display.*;
    import flash.utils.*;
    import flash.text.*;
    import flash.system.*;

    public class FrameRateGraph extends Sprite implements IRenderer {

        private static var nextColor:uint = 0;
        private static var _instance:FrameRateGraph;
        private static var debugBitmapDisplay:Bitmap = new Bitmap();
        private static var m_sprite:Sprite = new Sprite();

        private const BARS:uint = 60;
        private const MOVING_AVG_FRAMES:uint = 30;
        public const BAR_WIDTH:uint = 1;
        private const YELLOW_CUTOFF:uint = 50;
        private const RED_CUTOFF:uint = 100;

        private var m_tfFps:TextField;
        private var m_tfMovingAvgFps:TextField;
        private var m_lastTime:int;
        private var m_frameTimes:Array;
        public var m_frameBars:Array;
        private var m_bNoChart:Boolean = false;
        private var m_mem:String;
        private var m_secondBucket:int = 0;

        public function FrameRateGraph(){
            var _local3:int;
            var _local4:Shape;
            var _local5:Shape;
            var _local6:Shape;
            this.m_frameTimes = new Array();
            this.m_frameBars = new Array();
            super();
            _instance = this;
            m_sprite.addChild(debugBitmapDisplay);
            addChild(m_sprite);
            m_sprite.y = 10;
            var _local1:Shape = new Shape();
            _local1.graphics.lineStyle(2, 0xFFFFFF);
            _local1.graphics.beginFill(0);
            _local1.graphics.drawRoundRect(-5, (-(this.RED_CUTOFF) - 5), (this.m_bNoChart) ? 30 : ((this.BARS * this.BAR_WIDTH) + 10), (this.m_bNoChart) ? 45 : (this.RED_CUTOFF + 10), 5, 5);
            _local1.graphics.endFill();
            addChild(_local1);
            var _local2:int;
            while (_local2 < this.BARS) {
                this.m_frameTimes.push(10);
                _local2++;
            };
            if (!this.m_bNoChart){
                _local3 = 0;
                while (_local3 < this.BARS) {
                    _local6 = new Shape();
                    this.m_frameBars.push(_local6);
                    addChild(_local6);
                    _local3++;
                };
                _local4 = new Shape();
                _local4.graphics.lineStyle(1, 0xFFFF00);
                _local4.graphics.moveTo(0, -(this.YELLOW_CUTOFF));
                _local4.graphics.lineTo((this.BARS * this.BAR_WIDTH), -(this.YELLOW_CUTOFF));
                addChild(_local4);
                _local5 = new Shape();
                _local5.graphics.lineStyle(0, 0xFF0000);
                _local5.graphics.moveTo(0, -(this.RED_CUTOFF));
                _local5.graphics.lineTo((this.BARS * this.BAR_WIDTH), -(this.RED_CUTOFF));
                addChild(_local5);
            };
            this.m_tfFps = new TextField();
            this.m_tfFps.text = "9999999";
            this.m_tfFps.width = this.m_tfFps.textWidth;
            this.m_tfFps.textColor = 0xFFFFFF;
            this.m_tfFps.y = -100;
            this.m_tfFps.selectable = false;
            addChild(this.m_tfFps);
            this.m_tfMovingAvgFps = new TextField();
            this.m_tfMovingAvgFps.text = "9999999999";
            this.m_tfMovingAvgFps.width = this.m_tfMovingAvgFps.textWidth;
            this.m_tfMovingAvgFps.textColor = 0xFFFFFF;
            this.m_tfMovingAvgFps.y = -80;
            this.m_tfMovingAvgFps.selectable = false;
            addChild(this.m_tfMovingAvgFps);
        }
        public static function annotate(_arg1:uint):void{
            nextColor = _arg1;
        }
        public static function attach(_arg1:BitmapData):void{
            debugBitmapDisplay.bitmapData = _arg1;
        }
        public static function get debugSprite():Sprite{
            return (m_sprite);
        }

        public function activate():void{
            RenderManager.addFrameBeginUpdater(this);
        }
        public function deactivate():void{
            RenderManager.removeFrameBeginUpdater(this);
        }
        public function onRender(_arg1:int, _arg2:int):Boolean{
            var _local7:Shape;
            var _local8:uint;
            var _local3:int = getTimer();
            var _local4:int = (_local3 - this.m_lastTime);
            this.m_lastTime = _local3;
            this.m_secondBucket = (this.m_secondBucket + _local4);
            this.m_mem = (Number(((System.totalMemory / 0x0400) / 0x0400)).toFixed(1) + "mb");
            if (this.m_secondBucket > 1000){
                this.m_secondBucket = 0;
            };
            this.m_frameTimes.push(_local4);
            this.m_frameTimes.shift();
            var _local5:int;
            var _local6:int = (this.BARS - this.MOVING_AVG_FRAMES);
            while (_local6 < this.BARS) {
                _local5 = (_local5 + this.m_frameTimes[_local6]);
                _local6++;
            };
            _local5 = (_local5 / this.MOVING_AVG_FRAMES);
            if (!this.m_bNoChart){
                _local7 = this.m_frameBars.shift();
                _local7.graphics.clear();
                if (nextColor){
                    _local8 = nextColor;
                    nextColor = 0;
                } else {
                    if (_local4 >= this.RED_CUTOFF){
                        _local8 = 0xFF0000;
                    } else {
                        if (_local4 >= this.YELLOW_CUTOFF){
                            _local8 = 0xFFFF00;
                        } else {
                            _local8 = 0xCC00;
                        };
                    };
                };
                _local7.graphics.lineStyle(this.BAR_WIDTH, _local8);
                _local7.graphics.moveTo(0, -(_local4));
                _local7.graphics.lineTo(0, 0);
                _local7.graphics.lineStyle(0, 0xFFFFFF);
                _local7.graphics.moveTo(0, -(_local5));
                _local7.graphics.lineTo(this.BAR_WIDTH, -(_local5));
                this.m_frameBars.push(_local7);
                _local6 = 0;
                while (_local6 < this.BARS) {
                    _local7 = (this.m_frameBars[_local6] as Shape);
                    _local7.x = (_local6 * this.BAR_WIDTH);
                    _local6++;
                };
            };
            this.m_tfFps.text = (_local4.toString() + "ms/f");
            this.m_tfMovingAvgFps.text = ((Math.floor((1000 / _local5)).toString() + " | ") + this.m_mem);
            return (true);
        }

    }
}//package com.zynga.skelly.render 
